<template>
<view>
	<view class="top_div">
		<view class="">
			<image src="~@/static/image/rx_logo.png" mode="widthFix"></image>
			<view class="top_text">欢迎使用投资市政府服务热线</view>
		</view>
	</view>
	<view class="content">
		<view class="ps">温馨提示：带*号的为必填项，不带*号的为非必填项</view>
		<view class="cell">
			<view class="cell_name">诉求类型 <text class="must" style="color: red;">*</text></view>
			<picker @change="change1" :value="index1" :range="arr1" range-key='text'>
				<view class="cell_right" :class="index1==0?'cell_right_n':''">{{index1>=1?arr1[index1].text:'请选择诉求类型'}}</view>
			</picker>
		</view>
		<view class="cell">
			<view class="cell_name">诉求人姓名 <text class="must" style="color: red;">*</text></view>
			<input class="cell_right" v-model="data.name" placeholder="请输入诉求人姓名"/>
		</view>
		<view class="cell">
			<view class="cell_name">诉求人姓别 <text class="must" style="color: red;">*</text></view>
			<picker @change="change2" :value="index2" :range="arr2" range-key='text'>
				<view class="cell_right" :class="index2==0?'cell_right_n':''">{{index2>=1?arr2[index2].text:'请选择诉求人姓别'}}</view>
			</picker>
		</view>
		<view class="cell">
			<view class="cell_name">诉求人年龄 <text class="must" style="color: red;">*</text></view>
			<picker @change="change3" :value="index3" :range="arr3" range-key='text'>
				<view class="cell_right" :class="index3==0?'cell_right_n':''">{{index3>=1?arr3[index3].text:'请选择诉求人年龄'}}</view>
			</picker>
		</view>
		<view class="cell">
			<view class="cell_name">诉求人电话 <text class="must" style="color: red;">*</text></view>
			<input class="cell_right" v-model="data.phone" placeholder="请输入诉求人电话"/>
		</view>
		<view class="cell">
			<view class="cell_name">诉求主题 <text class="must" style="color: red;">*</text></view>
			<input class="cell_right" v-model="data.title" placeholder="请输入诉求主题"/>
		</view>
		<view class="cell">
			<view class="cell_name">诉求所属区域 <text class="must" style="color: red;">*</text></view>
			<picker @change="change4" :value="index4" :range="arr4" range-key='text'>
				<view class="cell_right" :class="index4==0?'cell_right_n':''">{{index4>=1?arr4[index4].text:'请选择诉求所属区域'}}</view>
			</picker>
		</view>
		<view class="cell">
			<view class="cell_name">诉求发生地址</view>
			<input class="cell_right" v-model="data.addarea" placeholder="请输入诉求发生地址"/>
		</view>
		<view class="cell cell_1">
			<view class="cell_name">诉求内容 <text class="must" style="color: red;">*</text></view>
			<textarea placeholder="请输入诉求内容" v-model="data.content"></textarea>
		</view>
		<view class="btn" @click="submit">
			提交
		</view>
	</view>
</view>
</template>

<script>
import CryptoJS from "crypto-js"
import {getHttpDataFromWeb} from '@/utils/api'
var C = CryptoJS;
var C_lib = C.lib;
var BlockCipher = C_lib.BlockCipher;
var C_algo = C.algo;

// Lookup tables
var SBOX = [];
var INV_SBOX = [];
var SUB_MIX_0 = [];
var SUB_MIX_1 = [];
var SUB_MIX_2 = [];
var SUB_MIX_3 = [];
var INV_SUB_MIX_0 = [];
var INV_SUB_MIX_1 = [];
var INV_SUB_MIX_2 = [];
var INV_SUB_MIX_3 = [];

// Compute lookup tables
(function () {
		// Compute double table
		var d = [];
		for (var i = 0; i < 256; i++) {
				if (i < 128) {
						d[i] = i << 1;
				} else {
						d[i] = (i << 1) ^ 0x11b;
				}
		}

		// Walk GF(2^8)
		var x = 0;
		var xi = 0;
		for (var i = 0; i < 256; i++) {
				// Compute sbox
				var sx = xi ^ (xi << 1) ^ (xi << 2) ^ (xi << 3) ^ (xi << 4);
				sx = (sx >>> 8) ^ (sx & 0xff) ^ 0x63;
				SBOX[x] = sx;
				INV_SBOX[sx] = x;

				// Compute multiplication
				var x2 = d[x];
				var x4 = d[x2];
				var x8 = d[x4];

				// Compute sub bytes, mix columns tables
				var t = (d[sx] * 0x101) ^ (sx * 0x1010100);
				SUB_MIX_0[x] = (t << 24) | (t >>> 8);
				SUB_MIX_1[x] = (t << 16) | (t >>> 16);
				SUB_MIX_2[x] = (t << 8)  | (t >>> 24);
				SUB_MIX_3[x] = t;

				// Compute inv sub bytes, inv mix columns tables
				var t = (x8 * 0x1010101) ^ (x4 * 0x10001) ^ (x2 * 0x101) ^ (x * 0x1010100);
				INV_SUB_MIX_0[sx] = (t << 24) | (t >>> 8);
				INV_SUB_MIX_1[sx] = (t << 16) | (t >>> 16);
				INV_SUB_MIX_2[sx] = (t << 8)  | (t >>> 24);
				INV_SUB_MIX_3[sx] = t;

				// Compute next counter
				if (!x) {
						x = xi = 1;
				} else {
						x = x2 ^ d[d[d[x8 ^ x2]]];
						xi ^= d[d[xi]];
				}
		}
}())

// Precomputed Rcon lookup
var RCON = [0x00, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36];

/**
 * AES block cipher algorithm.
 */
var AES = C_algo.AES = BlockCipher.extend({
		_doReset: function () {
				// Shortcuts
				var key = this._key;
				var keyWords = key.words;
				var keySize = key.sigBytes / 4;

				// Compute number of rounds
				var nRounds = this._nRounds = keySize + 6

				// Compute number of key schedule rows
				var ksRows = (nRounds + 1) * 4;

				// Compute key schedule
				var keySchedule = this._keySchedule = [];
				for (var ksRow = 0; ksRow < ksRows; ksRow++) {
						if (ksRow < keySize) {
								keySchedule[ksRow] = keyWords[ksRow];
						} else {
								var t = keySchedule[ksRow - 1];

								if (!(ksRow % keySize)) {
										// Rot word
										t = (t << 8) | (t >>> 24);

										// Sub word
										t = (SBOX[t >>> 24] << 24) | (SBOX[(t >>> 16) & 0xff] << 16) | (SBOX[(t >>> 8) & 0xff] << 8) | SBOX[t & 0xff];

										// Mix Rcon
										t ^= RCON[(ksRow / keySize) | 0] << 24;
								} else if (keySize > 6 && ksRow % keySize == 4) {
										// Sub word
										t = (SBOX[t >>> 24] << 24) | (SBOX[(t >>> 16) & 0xff] << 16) | (SBOX[(t >>> 8) & 0xff] << 8) | SBOX[t & 0xff];
								}

								keySchedule[ksRow] = keySchedule[ksRow - keySize] ^ t;
						}
				}

				// Compute inv key schedule
				var invKeySchedule = this._invKeySchedule = [];
				for (var invKsRow = 0; invKsRow < ksRows; invKsRow++) {
						var ksRow = ksRows - invKsRow;

						if (invKsRow % 4) {
								var t = keySchedule[ksRow];
						} else {
								var t = keySchedule[ksRow - 4];
						}

						if (invKsRow < 4 || ksRow <= 4) {
								invKeySchedule[invKsRow] = t;
						} else {
								invKeySchedule[invKsRow] = INV_SUB_MIX_0[SBOX[t >>> 24]] ^ INV_SUB_MIX_1[SBOX[(t >>> 16) & 0xff]] ^
																					 INV_SUB_MIX_2[SBOX[(t >>> 8) & 0xff]] ^ INV_SUB_MIX_3[SBOX[t & 0xff]];
						}
				}
		},

		encryptBlock: function (M, offset) {
				this._doCryptBlock(M, offset, this._keySchedule, SUB_MIX_0, SUB_MIX_1, SUB_MIX_2, SUB_MIX_3, SBOX);
		},

		decryptBlock: function (M, offset) {
				// Swap 2nd and 4th rows
				var t = M[offset + 1];
				M[offset + 1] = M[offset + 3];
				M[offset + 3] = t;

				this._doCryptBlock(M, offset, this._invKeySchedule, INV_SUB_MIX_0, INV_SUB_MIX_1, INV_SUB_MIX_2, INV_SUB_MIX_3, INV_SBOX);

				// Inv swap 2nd and 4th rows
				var t = M[offset + 1];
				M[offset + 1] = M[offset + 3];
				M[offset + 3] = t;
		},

		_doCryptBlock: function (M, offset, keySchedule, SUB_MIX_0, SUB_MIX_1, SUB_MIX_2, SUB_MIX_3, SBOX) {
				// Shortcut
				var nRounds = this._nRounds;

				// Get input, add round key
				var s0 = M[offset]     ^ keySchedule[0];
				var s1 = M[offset + 1] ^ keySchedule[1];
				var s2 = M[offset + 2] ^ keySchedule[2];
				var s3 = M[offset + 3] ^ keySchedule[3];

				// Key schedule row counter
				var ksRow = 4;

				// Rounds
				for (var round = 1; round < nRounds; round++) {
						// Shift rows, sub bytes, mix columns, add round key
						var t0 = SUB_MIX_0[s0 >>> 24] ^ SUB_MIX_1[(s1 >>> 16) & 0xff] ^ SUB_MIX_2[(s2 >>> 8) & 0xff] ^ SUB_MIX_3[s3 & 0xff] ^ keySchedule[ksRow++];
						var t1 = SUB_MIX_0[s1 >>> 24] ^ SUB_MIX_1[(s2 >>> 16) & 0xff] ^ SUB_MIX_2[(s3 >>> 8) & 0xff] ^ SUB_MIX_3[s0 & 0xff] ^ keySchedule[ksRow++];
						var t2 = SUB_MIX_0[s2 >>> 24] ^ SUB_MIX_1[(s3 >>> 16) & 0xff] ^ SUB_MIX_2[(s0 >>> 8) & 0xff] ^ SUB_MIX_3[s1 & 0xff] ^ keySchedule[ksRow++];
						var t3 = SUB_MIX_0[s3 >>> 24] ^ SUB_MIX_1[(s0 >>> 16) & 0xff] ^ SUB_MIX_2[(s1 >>> 8) & 0xff] ^ SUB_MIX_3[s2 & 0xff] ^ keySchedule[ksRow++];

						// Update state
						s0 = t0;
						s1 = t1;
						s2 = t2;
						s3 = t3;
				}

				// Shift rows, sub bytes, add round key
				var t0 = ((SBOX[s0 >>> 24] << 24) | (SBOX[(s1 >>> 16) & 0xff] << 16) | (SBOX[(s2 >>> 8) & 0xff] << 8) | SBOX[s3 & 0xff]) ^ keySchedule[ksRow++];
				var t1 = ((SBOX[s1 >>> 24] << 24) | (SBOX[(s2 >>> 16) & 0xff] << 16) | (SBOX[(s3 >>> 8) & 0xff] << 8) | SBOX[s0 & 0xff]) ^ keySchedule[ksRow++];
				var t2 = ((SBOX[s2 >>> 24] << 24) | (SBOX[(s3 >>> 16) & 0xff] << 16) | (SBOX[(s0 >>> 8) & 0xff] << 8) | SBOX[s1 & 0xff]) ^ keySchedule[ksRow++];
				var t3 = ((SBOX[s3 >>> 24] << 24) | (SBOX[(s0 >>> 16) & 0xff] << 16) | (SBOX[(s1 >>> 8) & 0xff] << 8) | SBOX[s2 & 0xff]) ^ keySchedule[ksRow++];

				// Set output
				M[offset]     = t0;
				M[offset + 1] = t1;
				M[offset + 2] = t2;
				M[offset + 3] = t3;
		},

		keySize: 256/32
});

/**
 * Shortcut functions to the cipher's object interface.
 *
 * @example
 *
 *     var ciphertext = CryptoJS.AES.encrypt(message, key, cfg);
 *     var plaintext  = CryptoJS.AES.decrypt(ciphertext, key, cfg);
 */
C.AES = BlockCipher._createHelper(AES);
// import a from '@/utils/aes.js'
function getNum() {
  var num = "";
  for (let i = 0; i < 5; i++) {
   num += Math.floor(Math.random() * 10);
  }
  return num;
 }

 function getTime() {
  var timestamp = new Date();
  var year = timestamp.getFullYear().toString();
  var month = (timestamp.getMonth() + 1).toString();
  var date = timestamp.getDate().toString();
  var dateStr = year.substring(year.length - 2, year.length) + month + date;
  return dateStr;
 }
function encrypt(str) {
	console.log(str)
  var key = CryptoJS.enc.Utf8.parse('gOuWYBjME1cVtc5S'); //将秘钥转换成Utf8字节数组
  let srcs = CryptoJS.enc.Utf8.parse(JSON.stringify(str));
  var encrypted = CryptoJS.AES.encrypt(srcs, key, {
   mode: CryptoJS.mode.ECB,
   padding: CryptoJS.pad.Pkcs7
  });
  var jiami = encrypted.ciphertext.toString().toUpperCase();
  return jiami;
 }
	export default {
		data() {
			return {
				//年龄
				arr3 : [{
						value: "",
						text: "请选择"
					},{
						value: "10",
						text: "20以下"
					},
					{
						value: "20",
						text: "20~39"
					},
					{
						value: "30",
						text: "40~59"
					}, {
						value: "40",
						text: "40~59"
					}, {
						value: "70",
						text: "无法获取"
					}
				],
					//诉求类型
				arr1 : [{
						value: "",
						text: "请选择"
					},{
						value: "1",
						text: "咨询"
					},
					{
						value: "2",
						text: "求助"
					},
					{
						value: "3",
						text: "举报"
					}, {
						value: "4",
						text: "投诉"
					}, {
						value: "5",
						text: "建议"
					}, {
						value: "9",
						text: "其他"
					}
				],
				//诉求区域
				arr4:[{
						value: "",
						text: "请选择"
					},{
						value: "360701",
						text: "市直部门"
					},
					{
						value: "360702",
						text: "章贡区"
					},
					{
						value: "360703",
						text: "南康区"
					}, {
						value: "360704",
						text: "赣县区"
					}, {
						value: "360722",
						text: "信丰县"
					}, {
						value: "360723",
						text: "大余县"
					}, {
						value: "360781",
						text: "瑞金市"
					}, {
						value: "360724",
						text: "上犹县"
					}, {
						value: "360725",
						text: "崇义县"
					}, {
						value: "360726",
						text: "安远县"
					}, {
						value: "360727",
						text: "龙南县"
					}, {
						value: "360728",
						text: "定南县"
					}, {
						value: "360729",
						text: "全南县"
					}, {
						value: "360791",
						text: "经济技术开发区"
					}, {
						value: "360730",
						text: "宁都县"
					}, {
						value: "360731",
						text: "于都县"
					}, {
						value: "360732",
						text: "兴国县"
					}, {
						value: "360733",
						text: "会昌县"
					}, {
						value: "360734",
						text: "寻乌县"
					}, {
						value: "360735",
						text: "石城县"
					}, {
						value: "360792",
						text: "容江新区"
					}
				],
					//性别
				arr2:[{
						value: "",
						text: "请选择"
					},{
						value: "男",
						text: "男"
					},
					{
						value: "女",
						text: "女"
					}
				],
				index1:0,
				index2:0,
				index3:0,
				index4:0,
				data:{
					serviceNo: '',
					name: '',
					phone: '',
					age: '',
					sex: '',
					serviceType: '',
					title: '',
					content:'',
					serviceArea: '',
					addarea: ''
				}
			}
		},
		methods: {
			change1(e){
				this.index1 = e.detail.value
				this.data.serviceType = this.arr1[this.index1].value
			},
			change2(e){
				this.index2 = e.detail.value
				this.data.sex = this.arr2[this.index2].value
			},
			change3(e){
				this.index3 = e.detail.value
				this.data.age = this.arr3[this.index3].value
			},
			change4(e){
				this.index4 = e.detail.value
				this.data.serviceArea = this.arr4[this.index4].value
			},
			submit(){
				this.data.serviceNo = "AP" + this.data.serviceArea + getTime() + getNum();
				let msg = ''
				if(!this.data.serviceType){
					msg = '请选择诉求类型'
				}
				if(!this.data.name){
					msg = '请输入姓名'
				}
				if(!this.data.sex){
					msg = '请输入性别'
				}
				if(!this.data.age){
					msg = '请输入年龄段'
				}
				if(!this.data.title){
					msg = '请输入诉求主题'
				}
				if(!this.data.serviceArea){
					msg = '请输入诉求区域'
				}
				if(!(/^1[3456789]\d{9}$/.test(this.data.phone))){
					msg = '请输入正确的手机号码'
				}
				if(!this.data.content){
					msg = '请输入诉求内容'
				}
				if(msg){
					return uni.showToast({
						title:msg,
						icon:'none'
					})
				}
				var encryptData=encrypt(this.data)
				getHttpDataFromWeb({
					data:JSON.stringify({'params':encryptData}),
					request_type:'post',
					request_type:'post',
					url:'aHR0cDovLzExMS43NS4yNTUuNzUvam1wb3J0YWxfeGN4L2ludGVyZmFjZXMvY2FsbC9hZGRzZXJ2aWNlLmRv',
					http_build:1
				}).then(res=>{
					if(res.data.data.success){
						uni.showToast({
							title:'提交成功',
							icon:'success'
						})
						setTimeout(()=>{
							uni.switchTab({
								url:'/pages/index/index'
							})
						},1500)
					}else{
						uni.showToast({
							title:res.data.data.msg,
							icon:'none'
						})
					}
				})
			}
		}
	}
</script>

<style scoped lang="scss">
.top_div{
	background: #148feb;
	height: 80px;
	display: flex;
	align-items: center;
	padding-left: 20px;
	image{
		width: 84px;
	}
	.top_text{
		font-size: 12px;
		margin-top: 2px;
		color: #fff;
	}
}
.content{
	.ps{
		font-size: 12px;
		color: #888;
		padding: 5px 20px;
	}
}
.cell{
	height: 40px;
	display: flex;
	align-items: center;
	padding: 0 20px;
	width: calc(100% - 40px);
	border-bottom: 1px solid #eee;
	justify-content: space-between;
	.cell_name{
		font-size: 14px;
		color: #363636;
		.must{
			position: relative;
			top: 3px;
		}
	}
	.cell_right{
		font-size: 14px;
		text-align: right;
	}
	.cell_right_n{
		color: #888;
	}
}
.cell_1{
	height: 120px;
	textarea{
		height: calc(100% - 20px);
		font-size: 14px;
		width: calc(100% - 100px);
		padding: 10px 0;
	}
}
.btn{
	height: 40px;
	width: 80%;
	text-align: center;
	background-color: #4394f4;
	border-radius: 5px;
	cursor: pointer;
	margin: 0 10%;
	color: #fff;
	font-size: 16px;
	line-height: 40px;
	margin-top: 40px;
}
</style>
